package com.lijiajian.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lijiajian.base.BaseService;
import com.lijiajian.dao.CustomerMapper;
import com.lijiajian.dao.CustomerServeMapper;
import com.lijiajian.dao.UserMapper;
import com.lijiajian.enums.CustomerServeStatus;
import com.lijiajian.pojo.CustomerOrder;
import com.lijiajian.pojo.CustomerServe;
import com.lijiajian.query.CustomerOrderQuery;
import com.lijiajian.query.CustomerServeQuery;
import com.lijiajian.utils.AssertUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class CustomerServeService extends BaseService<CustomerServe,Integer> {
    @Resource
    private CustomerServeMapper customerServeMapper;
    @Resource
    private CustomerMapper  customerMapper;
    @Resource
    private UserMapper userMapper;

    /**
     * 多条件查询客户服务
     * @param
     * @return
     */
    public Map<String,Object> queryCustomerServeByParams(CustomerServeQuery customerServeQuery){
        Map<String,Object> map=new HashMap<>();
        //开启分页
        PageHelper.startPage(customerServeQuery.getPage(),customerServeQuery.getLimit());
        PageInfo<CustomerServe> pageInfo=new PageInfo<>(customerServeMapper.selectByParams(customerServeQuery));
        map.put("code",0);
        map.put("msg","success");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());

        return map;
    }




    @Transactional(propagation = Propagation.REQUIRED)
    public void addCustomerServe(CustomerServe customerServe){
        AssertUtil.isTrue(StringUtils.isBlank(customerServe.getCustomer()),"客户名不能为空");

        AssertUtil.isTrue(customerMapper.queryCustomerByName(customerServe.getCustomer())==null,"客户记录不存在");

        AssertUtil.isTrue(customerServe.getServeType()==null,"请选择客户类型");

        AssertUtil.isTrue(StringUtils.isBlank(customerServe.getServiceRequest()),"服务请求内容不能为空");

        //设置默认值
        customerServe.setState(CustomerServeStatus.CREATED.getState());
        customerServe.setIsValid(1);
        customerServe.setCreateDate(new Date());
        customerServe.setUpdateDate(new Date());
        customerServe.setFlag(0);//默认值为审核不通过

        AssertUtil.isTrue(customerServeMapper.insertSelective(customerServe)!=1,"添加失败");
    }


    @Transactional(propagation = Propagation.REQUIRED)
    public void updateFlag(String state,Integer id){
        AssertUtil.isTrue(customerServeMapper.updateFlagByStateAndId(state,id)!=1,"审核失败");
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteCustomerServe(Integer id){
        //客户服务id非空且记录存在
        AssertUtil.isTrue(id==null||customerServeMapper.selectByPrimaryKey(id)==null,"待删除记录不存在");
        AssertUtil.isTrue(customerServeMapper.deleteCustomerServeById(id)!=1,"删除失败");
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void updateCustomerServe(CustomerServe customerServe){
        //客户服务id非空且记录存在
        AssertUtil.isTrue(customerServe.getId()==null||customerServeMapper.selectByPrimaryKey(customerServe.getId())==null,"待更新记录不存在");
        //判断客户服务的服务状态
        if(CustomerServeStatus.CREATED.getState().equals(customerServe.getState())){
            AssertUtil.isTrue(StringUtils.isBlank(customerServe.getCustomer()),"客户名不能为空");

            AssertUtil.isTrue(customerMapper.queryCustomerByName(customerServe.getCustomer())==null,"客户记录不存在");

            AssertUtil.isTrue(customerServe.getServeType()==null,"请选择客户类型");

            AssertUtil.isTrue(StringUtils.isBlank(customerServe.getServiceRequest()),"服务请求内容不能为空");


        }else if(CustomerServeStatus.ASSIGNED.getState().equals(customerServe.getState())){
                //服务分配
            //分配人 非空且用户记录存在
            AssertUtil.isTrue(StringUtils.isBlank(customerServe.getAssigner()),"待分配用户不能为空");
            AssertUtil.isTrue(userMapper.selectByPrimaryKey(Integer.parseInt(customerServe.getAssigner()))==null,"分配用户记录不存在");
            customerServe.setAssignTime(new Date());


        }
        else if(CustomerServeStatus.PROCED.getState().equals(customerServe.getState())){
                //服务处理
            AssertUtil.isTrue(StringUtils.isBlank(customerServe.getServiceProce()),"服务处理内容不能为空");
            customerServe.setServiceProceTime(new Date());

        }
        else if(CustomerServeStatus.FEED_BACK.getState().equals(customerServe.getState())){
                //服务反馈
            AssertUtil.isTrue(StringUtils.isBlank(customerServe.getServiceProceResult()),"服务反馈内容不能为空");
            AssertUtil.isTrue(StringUtils.isBlank(customerServe.getMyd()),"请选择服务反馈满意度");
            customerServe.setState(CustomerServeStatus.ARCHIVED.getState());
        }
        //设置默认值
        customerServe.setUpdateDate(new Date());

        AssertUtil.isTrue(customerServeMapper.updateByPrimaryKeySelective(customerServe)!=1,"修改失败");

    }









}
